/**
 *  Copyright � 2010 Digia Plc
 *  Copyright � 2010 Nokia Corporation
 *
 *  All rights reserved.
 *
 *  Nokia and Nokia Connecting People are registered trademarks of
 *  Nokia Corporation.
 *  Java and all Java-based marks are trademarks or registered
 *  trademarks of
 *  Sun Microsystems, Inc. Other product and company names
 *  mentioned herein may be
 *  trademarks or trade names of their respective owners.
 *
 *
 *  Subject to the conditions below, you may, without charge:
 *
 *  �  Use, copy, modify and/or merge copies of this software and
 *     associated documentation files (the "Software")
 *
 *  �  Publish, distribute, sub-licence and/or sell new software
 *     derived from or incorporating the Software.
 *
 *
 *  This file, unmodified, shall be included with all copies or
 *  substantial portions
 *  of the Software that are distributed in source code form.
 *
 *  The Software cannot constitute the primary value of any new
 *  software derived
 *  from or incorporating the Software.
 *
 *  Any person dealing with the Software shall not misrepresent
 *  the source of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 *  KIND, EXPRESS OR IMPLIED,
 *  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE
 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import Qt 4.7
import "../colibri/javascripts/functions.js" as Functions
import "javascript/timer.js" as ClockTimer
import "../colibri"

Rectangle {

    id: structure;

    property CLStyle style : CLStyle{}
    property real roundness : style.roundness
    property alias hours : textHours.text
    property alias minutes : textMinutes.text
    property int fontSize : 25
    property string selected:  "hours"
    property color textColorSelected: "red"
    property color textColor: style.textColor
    property bool pastClockTimesDisabled: false;
    property bool isCurrentDate: true;
    property int minuteInterval: 5

    function addMinute() {
        minutes = ClockTimer.formatTwoDigits(ClockTimer.parseIntClever(minutes) + minuteInterval);
        if (minutes  > 59) minutes = "00"
        if (isCurrentDate && getCurrentHour() >= ClockTimer.parseIntClever(hours)
            && getCurrentMinute() > ClockTimer.parseIntClever(minutes) )
            minutes = ClockTimer.formatTwoDigits( getCurrentMinute() );
    }


    /**
     *
     */
    function decreaseMinute() {
        minutes = ClockTimer.formatTwoDigits(ClockTimer.parseIntClever(minutes) - minuteInterval);
        if (minutes == "0-5" || ClockTimer.parseIntClever(minutes) < 0 ||
                isCurrentDate && ( isCurrentDate && getCurrentHour() >= ClockTimer.parseIntClever(hours)
                && ClockTimer.parseIntClever(minutes) < getCurrentMinute() )) {
            var tempMin = (60 - minuteInterval);
            minutes = ClockTimer.formatTwoDigits( tempMin );
        }
        if (isCurrentDate && getCurrentHour() >= ClockTimer.parseIntClever(hours)
            && getCurrentMinute() > ClockTimer.parseIntClever(minutes) )
            minutes = ClockTimer.formatTwoDigits( getCurrentMinute() );
    }

    function addHour() {
        if (hours == 23) hours = "00"
        else hours = ClockTimer.formatTwoDigits(ClockTimer.parseIntClever(hours) + 1);
        if (isCurrentDate && getCurrentHour() > ClockTimer.parseIntClever(hours) ) hours = ClockTimer.formatTwoDigits( getCurrentHour() );
        if (isCurrentDate && getCurrentHour() >= ClockTimer.parseIntClever(hours) && getCurrentMinute() > ClockTimer.parseIntClever(minutes) ) minutes = ClockTimer.formatTwoDigits( getCurrentMinute() );
        if (minutes == "60" ) {
            minutes = "00";
            addHour();
        }
    }

    function decreaseHour() {
        if (hours == 0 || ( isCurrentDate && ClockTimer.parseIntClever(hours) == getCurrentHour() )) hours = "23"
        else hours = ClockTimer.formatTwoDigits(ClockTimer.parseIntClever(hours) - 1);
        if (isCurrentDate && getCurrentHour() > ClockTimer.parseIntClever(hours) )
            hours = ClockTimer.formatTwoDigits( getCurrentHour() );
        if (isCurrentDate && getCurrentHour() >= ClockTimer.parseIntClever(hours)
                && getCurrentMinute() > ClockTimer.parseIntClever(minutes) )
            minutes = ClockTimer.formatTwoDigits( getCurrentMinute() );
        if (minutes == "60" ) {
            minutes = "00";
            addHour();
        }
    }

    function getCurrentHour() {
        var today = new Date();
        return today.getHours();
    }

    function getCurrentMinute() {
        var today = new Date();
        var theMinutes = today.getMinutes();
        theMinutes = ( (theMinutes % structure.minuteInterval) == 0 ) ? theMinutes : ((Math.floor(theMinutes/structure.minuteInterval)*structure.minuteInterval)+structure.minuteInterval);
        if (theMinutes<10) return "0"+theMinutes
        return theMinutes;
    }

    function checkTheTime() {
        if (getCurrentHour() >= ClockTimer.parseIntClever(hours) && getCurrentMinute() > ClockTimer.parseIntClever(minutes) ) minutes = ClockTimer.formatTwoDigits( getCurrentMinute() );
        if (getCurrentHour() > ClockTimer.parseIntClever(hours) ) hours = ClockTimer.formatTwoDigits( getCurrentHour() );
        if (minutes == "60" ) {
            minutes = "00";
            addHour();
        }
    }

    onMinuteIntervalChanged: {
        if (minuteInterval < 1 || minuteInterval > 30 ) minuteInterval = 1;
        if (((60/minuteInterval)%1) != 0 ) minuteInterval = 1;
    }

    width: 120
    height: 70
    radius: Functions.countRadius(roundness,width,height,0,1);
    smooth: true

    Timer {
        id: checkPastTime
        running: isCurrentDate
        interval: 500
        triggeredOnStart: isCurrentDate
        onTriggered: checkTheTime()
        repeat: true
    }

    Row {
        anchors.verticalCenter: parent.verticalCenter
            Text {
                id: textHours
                text: getCurrentHour()
                font.pointSize: 0.001 + fontSize
                color: textColorSelected
                width: structure.width*0.3
                anchors.verticalCenter: parent.verticalCenter

                MouseArea {
                    id: mouseAreaHours
                    anchors.fill: parent
                    onClicked: {
                        textHours.color = textColorSelected
                        textMinutes.color = textColor
                    }
                    hoverEnabled: true;
                }

            }

            Text {
                text: ":"
                font.pointSize: 0.001 + fontSize
                color: textColor
                width: structure.width*0.1
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                id: textMinutes
                text: getCurrentMinute()
                font.pointSize: 0.001 + fontSize
                color: textColor
                width: structure.width*0.3
                anchors.verticalCenter: parent.verticalCenter
                MouseArea {
                    id: mouseAreaMinutes
                    anchors.fill: parent
                    onClicked: {
                        textMinutes.color = textColorSelected
                        textHours.color = textColor
                    }
                    hoverEnabled: true;
                }
            }


        Column {
            CLButton{
                id: buttonUp
                style: structure.style
                text: ""
                backgroundImage: Image {source: "../colibri/images/arrow_up_50x50.png"; scale: structure.height*0.006; smooth: structure.smooth}
                width: structure.width*0.3
                fontSize: 0.00001 + structure.fontSize*0.5
                height: structure.height/2
                smooth: structure.smooth
                onClicked: {
                    if (textHours.color == textColorSelected) addHour()
                    else addMinute()
                }
            }
            CLButton{
                id: buttonDown
                style: structure.style
                text: ""
                backgroundImage: Image {source: "../colibri/images/arrow_down_50x50.png"; scale: structure.height*0.006; smooth: structure.smooth}
                width: structure.width*0.3
                fontSize: 0.00001 + structure.fontSize*0.5
                height: structure.height/2
                smooth: structure.smooth
                onClicked: {
                    if (textHours.color == textColorSelected) decreaseHour()
                    else decreaseMinute()
                }
            }
        }
     }
 }
